//
//  LTMineViewController.m
//  LearnTeacher
//
//  Created by zyl_macbookpro on 2024/4/9.
//

#import "LTMineViewController.h"
#import "LTMineTableViewCell.h"
#import "LTMineAPIManager.h"
@interface LTMineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *tableHeaderView;

@property(nonatomic ,strong)NSArray *titleArray;

@property(nonatomic ,strong)NSString *nickName;

@property(nonatomic ,strong)NSString *avatar;

@property(nonatomic ,strong)NSString *schoolName;

@property(nonatomic ,strong)NSString *gradeName;

@property(nonatomic ,strong)NSString *gradeVip;

@end

@implementation LTMineViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getUserInfo];
    [self setHidenNavigationBar:YES];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
   
    [self.view addSubview:self.tableView];
  
    
}
- (void)getUserInfo
{
    [LTMineAPIManager getUserInfoWithParam:@{} result:^(NSDictionary * _Nonnull dic) {
        NSDictionary *data = dic[@"data"];
        self.nickName = data[@"nickname"];
        self.avatar = data[@"avatar"];
        self.schoolName = data[@"school"][@"name"];
        self.gradeName = data[@"grade"][@"gradeName"];
//        lApp.schoolName = self.schoolName;
//        lApp.gradeName = self.gradeName;
        self.gradeVip = data[@""];
        self.tableHeaderView = nil;
       // [self getCustomInfo];
        [self.tableView setTableHeaderView:self.tableHeaderView];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LTMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineTableViewCell"];
    
    if (!cell)
    {
        cell = [[LTMineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MineTableViewCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    cell.data = self.titleArray[indexPath.row];
   
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titleArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 60;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"个人信息"]) {
        [self pushToViewControllerWithClassName:@"LTPersonInformationVC" animated:YES];
    }
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"联系我们"]) {
        [self pushToViewControllerWithClassName:@"LTConnecUsViewController" animated:YES];
    }
   
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"投诉"]) {
        [self pushToViewControllerWithClassName:@"LTComplainFeedBackVC" animated:YES];
    }
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"关于我们"]) {
        [self pushToViewControllerWithClassName:@"LTAboutUsViewController" animated:YES];
    }
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"注销账号"]) {
        [self pushToViewControllerWithClassName:@"LTAccountCancellationVC" animated:YES];
    }
    
    if ([[self.titleArray[indexPath.row] objectForKey:@"title"] isEqualToString:@"撤销同意声明"]) {
        [self pushToViewControllerWithClassName:@"LTAccountCancellationExplainVC" animated:YES];
    }
    
}
#pragma mark -event response

- (void)changeToProtectColor:(UISwitch *)switchP
{
//    [MineAPIManager ModiftyProtectEyesSettingWithParam:@{@"is_eyeshield":[NSNumber numberWithBool:switchP.on]} result:^(NSDictionary * _Nonnull dic) {
//        lApp.is_eyeshield = switchP.on;
//        USER_DEFAULTS_SAVE(ProtectColorSwitch, [NSNumber numberWithBool:lApp.is_eyeshield]);
//        [lApp turnOrOffProtectColor];
//        
//    }];
}
- (void)tapClick
{
    [self.view endEditing:YES];
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, -LTTopBarHeight, LTScreenWidth, LTScreenHeight) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:LTClearColor];
        [_tableView setSeparatorColor:LTClearColor];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
        [_tableView setTableHeaderView:self.tableHeaderView];
    }
    
    return _tableView;
}

-(UIView *)tableHeaderView
{
    if (!_tableHeaderView)
    {
        _tableHeaderView =[[UIView alloc] initWithFrame:CGRectMake(0, 0,  LTScreenWidth,320/375.0*LTScreenWidth - 13)];
        UIImageView *headerBgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,  LTScreenWidth, 320.0/375* LTScreenWidth)];
        headerBgImageView.image = IMAGENAMED(@"ic_mine_headerBg");
        [_tableHeaderView addSubview:headerBgImageView];
        UIView *cardView = [[UIView alloc] init];
        cardView.frame = CGRectMake(16,139.0/375.0*LTScreenWidth, LTScreenWidth - 32,143);
        cardView.backgroundColor =  LTWhiteColor;
        [cardView setAllCornersWithCornerRadius: 16];
        [_tableHeaderView addSubview:cardView];
      
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0,0,LTScreenWidth - 32,143);
        gl.startPoint = CGPointMake(0.5, 0);
        gl.endPoint = CGPointMake(0.5, 0.38);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:212/255.0 green:234/255.0 blue:255/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        [cardView.layer addSublayer:gl];
        
        UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 96/375.00* LTScreenWidth, 88, 88)];
        
        headerImageView.centerX = _tableHeaderView.centerX;
        if (self.avatar.length > 0) {
            NSURL *url = [NSURL URLWithString:self.avatar];
            if ([self.avatar hasPrefix:@"http"]) {
                
                [headerImageView sd_setImageWithURL:url];
                
            }else{
              
                NSData*data = [NSData dataWithContentsOfURL:url];
                UIImage *img = [UIImage imageWithData:data];
                headerImageView.image = img;
                
            }
           
        }
        
        UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(26, headerImageView.bottom + 16,  LTScreenWidth - 52, 30)];
        nameLabel.font = SFontWithSize(22);
        nameLabel.text = self.nickName.length > 0?self.nickName:@"";
        [nameLabel setTextAlignment:NSTextAlignmentCenter];
        nameLabel.textColor =  LTTextBlack;
        
        [headerImageView setAllCornersWithCornerRadius:44];
        
        [_tableHeaderView addSubview:nameLabel];
       
        [_tableHeaderView addSubview:headerImageView];
        
        UILabel * schoolLabel = [[UILabel alloc] initWithFrame:CGRectMake(26, nameLabel.bottom + 10,  LTScreenWidth - 52, 18)];
        schoolLabel.font = RFontWithSize(13);
        self.schoolName = self.schoolName.length > 0?self.schoolName:@"";
        self.gradeName = self.gradeName.length > 0?self.gradeName:@"";
        schoolLabel.text = [NSString stringWithFormat:@"%@ %@",self.schoolName,self.gradeName];
        [schoolLabel setTextAlignment:NSTextAlignmentCenter];
        schoolLabel.textColor = LTTextGray;
      
        [_tableHeaderView addSubview:schoolLabel];
        
        
    }
    
    return _tableHeaderView;
}

- (NSArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = @[@{@"title":@"个人信息",@"img":@"ic_mine_person"},@{@"title":@"联系我们",@"img":@"ic_mine_connect"},@{@"title":@"投诉",@"img":@"ic_mine_feedback"},@{@"title":@"注销账号",@"img":@"ic_mine_cancelAccount"},@{@"title":@"关于我们",@"img":@"ic_mine_about"},@{@"title":@"撤销同意声明",@"img":@"ic_mine_warn"}];
    }
    return _titleArray;
}

@end
